import Vue from 'vue'
import Router from 'vue-router'
import Home from "@/Home";
import index from "@/pages/index";
import articleCategory from "@/pages/articleCategory";
import articleTag from "@/pages/articleTag";
import articleCategoryPage from "@/pages/articleCategoryPage";
import articleView from "@/pages/articleView";
import register from "@/pages/register";
import login from "@/pages/login";
import store from '@/store';
import {Message} from 'element-ui';
import articleDiscover from "@/pages/articleDiscover";
import articleWrite from "@/pages/articleWrite";
import articleSearch from "@/pages/articleSearch";
import layout from "@/pages/admin/layout";
import userManagement from "@/pages/admin/userManagement";
import commentManagement from "@/pages/admin/commentManagement";
import articleManagement from "@/pages/admin/articleManagement";

Vue.use(Router)

const router = new Router({
    routes:[
        {
            path:'/',
            name:'Home',
            component:Home,
            children:[
                {
                    path:'',
                    name:'index',
                    component:index
                },
                {
                    path:'category',
                    name:'articleCategory',
                    component:articleCategory,
                    //todo 菜单分类页面下  子级路由 不能嵌套吗
                    /*children:[
                        {
                            path:'/detail',
                            name:'articleCategoryPage',
                            component:articleCategoryPage
                        }
                    ]*/
                },
                {
                    path:'category/:id',
                    name:'articleCategoryPage',
                    component:articleCategoryPage
                },
               {
                    path:'tag',
                    name:'articleTag',
                    component:articleTag,
                },
                 {
                    path:'discover',
                    name:'articleDiscover',
                    component:articleDiscover,
                },
                 {
                    path:'article/view/:id',
                    name:'articleView',
                    component:articleView,
                },
                 {
                    path:`article/search/:searchVal`,
                    name:'articleSearch',
                    component:articleSearch,
                }
            ]
        },
        {
            path: '/write',
            name:'articleWrite',
            component:articleWrite,
            meta: {
                isLogin: true
            },
        },
        {
            path:'/register',
            name:'register',
            component:register,
        },
        {
            path:'/login',
            name:'login',
            component:login,
        },
        {
            path: '/admin',
            name: 'layout',
            component: layout,
            children:[
                {
                    path:'user',
                    name:'userManagement',
                    component:userManagement
                },
                {
                    path:'article',
                    name:'articleManagement',
                    component:articleManagement
                },
                {
                    path:'comment',
                    name:'commentManagement',
                    component:commentManagement
                }
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    if (store.state.token) {
        if (to.path === "/login") {
            next({path: '/'})
        } else {
            if (store.state.account.length === 0) {
                store.dispatch("getUserInfo").then(data => { //获取用户信息
                    next();
                }).catch(() => {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: '登录过期'
                    })
                    next({path: '/login'})
                })
            } else {
                next()
            }
        }
    } else {
        if (to.matched.some(r => r.meta.isLogin)) {
            Message({
                type: 'warning',
                showClose: true,
                message: '请先登录账户'
            })
        }
        else {
            next();
        }
    }
})

export default router
